package com.novaed.game.lifeline;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class LifeLine extends Thread {

	private JFrame frame;

	public static JLabel lblTimer = new JLabel(" ");
	JButton btnTurtle = new JButton("Turtle Time");
	JButton btnFifty = new JButton("50/50");
	JButton btnSwitch = new JButton("Switch");
	JButton btnA = new JButton("A. Choice");
	JButton btnB = new JButton("B. Choice");
	JButton btnC = new JButton("C. Answer");
	JButton btnD = new JButton("D. Choice");
	
	static TurtleTimer2 start;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LifeLine window = new LifeLine();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		
		start = new TurtleTimer2("STARTER");
		start.run();
		
		// TODO disable choice buttons on timer = 0;
		
	}

	/**
	 * Create the application.
	 */
	public LifeLine() {

		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {

		final List<JButton> jb = new ArrayList<JButton>();
		
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JPanel panel_2 = new JPanel();
		frame.getContentPane().add(panel_2, BorderLayout.NORTH);
		panel_2.add(lblTimer);

		JPanel panel = new JPanel();
		frame.getContentPane().add(panel, BorderLayout.SOUTH);
		btnTurtle.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnTurtle.setEnabled(false);
				
				start.suspendTimer();
				final int wait = 8;
				new Thread(new Runnable(){

					@Override
					public void run() {

							
							try {
								for (int i = wait; i > 0; i--) {
									System.out.println(i);
									Thread.sleep(1000);
								}
								
							} catch (InterruptedException e) {
								System.err.println("** btnTurtle : Interrupted Exception **");
								System.err.println(" new Thread error ");
								e.printStackTrace();
							}
						
					}
					
				}).start();
			
				try {
					Thread.sleep(wait*1000);
					start.resumeTimer();
				} catch (InterruptedException ie) {
					System.err.println("** btnTurtle : Interrupted Exception **");
					System.err.println(" resumeTimer() error ");
					ie.printStackTrace();
				}
				
			}
		});

		panel.add(btnTurtle);

		// button getName == "CHOICE" add to jb
		jb.add(btnA);
		jb.add(btnB);
		jb.add(btnD);
		
		btnFifty.setName("CHOICE");
		btnFifty.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				int seed = new Random().nextInt(3);
				
				System.out.println(seed);
				
				if (seed == 0) {
					jb.get(0).setEnabled(false);
					jb.get(1).setEnabled(false);
				}
				
				if (seed == 1) {
					jb.get(1).setEnabled(false);
					jb.get(2).setEnabled(false);
				}
				
				if (seed == 2) {
					jb.get(0).setEnabled(false);
					jb.get(2).setEnabled(false);
				}
				
				btnFifty.setEnabled(false);
				
			}
		});
		panel.add(btnFifty);
		panel.add(btnSwitch);

		JPanel panel_1 = new JPanel();
		frame.getContentPane().add(panel_1, BorderLayout.CENTER);

		panel_1.add(btnA);
		panel_1.add(btnB);
		panel_1.add(btnC);
		panel_1.add(btnD);
	}

}

/**
 * @author  Glenn Mark
 */
class TurtleTimer2 extends LifeLine implements Runnable {

	private String name;
	private Thread t;
	private final int START_TIME= 3;
	private boolean suspendFlag;
	
	TurtleTimer2 (String threadname) {
		name = threadname;
		t = new Thread(this, name);
		System.out.println("New " + t);
		suspendFlag = false;
		t.start(); 
	}

	public void run() {
		
		try {
			
			for (int i = START_TIME; i >= 0; i--) {
				
				lblTimer.setText(String.valueOf(i));
				Thread.sleep(1000);
				
				synchronized (this) {
					
					while (suspendFlag) {
						wait();
					}
					
				}
			}
			
			// TODO 0-end
			
		} catch (InterruptedException e) {
			System.err.println(name + " has been interrupted.");
		}
	}

	public void suspendTimer() {
		
		suspendFlag = true;
	}

	synchronized void resumeTimer() {
		
		suspendFlag = false;
		notify();
	}

}